﻿using System;
using System.ComponentModel.DataAnnotations;
using XCI.Extensions;
using XCI.Sys.Model;

namespace XCI.Sys.Validator
{
    /// <summary>
    /// 新建用户 是否选择企业
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
    public class NewUserEntIdAttribute : ValidationAttribute
    {
        /// <summary>
        /// 确定指定的值的对象是否有效。
        /// </summary>
        /// <param name="value">要验证的对象的值</param>
        /// <param name="validationContext">验证上下文</param>
        /// <returns>true 如果指定的值是否有效，则为否则为 false。</returns>
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value == null) return ValidationResult.Success;
            var user = (SysUser)value;

            if (user.Category.IsEmpty())
            {
                return new ValidationResult("请选择账户类型");
            }

            if (!user.Category.Equals(0))
            {
                if (user.EntId.IsEmpty())
                {
                    return new ValidationResult("请选择企业");
                }
            }
            return ValidationResult.Success;
        }
    }
}
